package com.cy.ppj.web.front.user;

import com.cy.ppj.model.CacheBean;
import com.cy.ppj.model.domian.account.Commission;
import com.cy.ppj.model.dto.sys.BrokerCommissionSettingsDTO;
import com.cy.ppj.model.dto.sys.TeamCommissionSettingsDTO;
import com.cy.ppj.model.enums.account.BrokerLevel;
import com.cy.ppj.model.query.account.TeamMemberQuery;
import com.cy.ppj.model.query.dealrecord.DealRecordQuery;
import com.cy.ppj.model.vo.front.account.*;
import com.cy.ppj.model.vo.front.dealrecord.DealRecordVO;
import com.cy.ppj.service.account.*;
import com.cy.ppj.service.dealrecord.DealRecordService;
import com.cy.ppj.service.sys.SystemSettingsService;
import com.cy.ppj.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.withdraw.WithdrawApplyAO;
import com.cy.ppj.model.domian.account.Withdraw;
import com.cy.ppj.model.enums.SmsType;
import com.cy.ppj.model.query.CommissionRecordQuery;
import com.cy.ppj.model.query.WithdrawQuery;
import com.cy.ppj.model.vo.CommissionRecordVO;
import com.cy.ppj.service.withdraw.WithdrawService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 前台-我的账户
 *
 * @author: tanyilin
 * @create: 2018年7月24日 下午8:57:37
 */
@Slf4j
@RestController
@RequestMapping("/user/account")
@Api(tags = "前台-我的账户")
public class UserAccountController extends BaseController {

    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private DealRecordService dealRecordService;
    @Autowired
    private CommissionService commissionService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeamCommissionService teamCommissionService;
    @Autowired
    private CommissionRecordService commissionRecordService;
    @Autowired
    private BrokerService brokerService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SystemSettingsService systemSettingsService;

    @ApiOperation("首页(修改)")
    @GetMapping("/index")
    public Result<AccountIndexVO> index() {
        AccountIndexVO accountIndex = userService.queryAccountIndexInfo(this.getUser().getUserId());
        return ok(null, accountIndex);
    }


    @ApiOperation("提现申请(修改)")
    @PostMapping("/withdraw/apply")
    public Result withdrawApply(@Validated WithdrawApplyAO withdrawApplyAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        this.checkSmsCode(withdrawApplyAO.getBankMobile(), withdrawApplyAO.getCode(), SmsType.WITHDRAW);
        withdrawService.withdrawApply(withdrawApplyAO, this.getUser().getUserId());
        return ok();
    }

    @ApiOperation("提现记录")
    @GetMapping("/withdraw/list")
    public PageBean<Withdraw> withdrawList(WithdrawQuery withdrawQuery) {
        withdrawQuery.setUserId(this.getUser().getUserId());
        return withdrawService.queryWithdtawList(withdrawQuery);
    }

    @ApiOperation("佣金记录(修改)")
    @GetMapping("/commission/record")
    public PageBean<CommissionRecordVO> commissionRecord(Integer type) {
        CommissionRecordQuery query = new CommissionRecordQuery();
        query.setUserId(this.getUser().getUserId());
        query.setType(type);
        return commissionRecordService.commissionRecord(query);
    }

    @ApiOperation("经纪人(小精灵页面)(修改)")
    @GetMapping("/broker/info")
    public Result<BrokerInfoVO> brokerInfo() {
        Long userId = this.getUser().getUserId();

        BrokerInfoVO brokerInfo = brokerService.queryBrokerInfo2(userId);

        return ok(null, brokerInfo);
    }

    @ApiOperation("经纪人-放款统计")
    @GetMapping("/broker/loan/statistics")
    public Result<CacheBean<UserLoanStatisticsVO>> brokerLoanStatistics(@RequestParam Integer type) {

        Long userId = this.getUser().getUserId();
        CacheBean<UserLoanStatisticsVO> cacheBean = null;
        Date time = new Date();

        switch (type) {
            case 1:
                cacheBean = brokerService.queryUserLoanMonthStatisticsInCache(userId, time);
                break;

            case 2:
                cacheBean = brokerService.queryUserLoanQuarterStatisticsInCache(userId, time);
                break;

            default:
                break;
        }
        return ok(null, cacheBean);
    }

    @ApiOperation("我的佣金(修改)")
    @GetMapping("/my/commission")
    public Result<CommissionInfoVO> myCommission() {

        Long userId = this.getUser().getUserId();
        CommissionInfoVO commissionInfo = commissionRecordService.queryUserCommissionInfo2(userId);
        Date time = new Date();
        CacheBean<CommissionStatisticsVO> list = commissionRecordService.commissionStatistics(userId, time);
        commissionInfo.setCommissionStatistics(list.getList());
        commissionInfo.setCacheTime(list.getCacheTime());

        return ok(null, commissionInfo);
    }

    @ApiOperation("我的团队-成员（合伙人列表）(修改)")
    @GetMapping("/my/team/members")
    public PageBean<TeamMemberVO> myTeamMembers(TeamMemberQuery query) {
        query.setUserId(this.getUser().getUserId());
        return teamCommissionService.queryTeamMembers(query);
    }

    @ApiOperation("我的团队详情(修改)")
    @GetMapping("/my/team")
    public Result<MyTeamVO> myTeam() {

        Long userId = this.getUser().getUserId();

        //获取团队累计返佣，和订单统计
        MyTeamVO myTeamVO = teamCommissionService.queryMyTeamInfo2(userId);


        return ok(null, myTeamVO);
    }

    @ApiOperation("团队数据统计")
    @GetMapping("/my/team/statistics")
    public Result<MyTeamStatisticsVO> myTeamStatistics(@RequestParam Integer type) {
        MyTeamStatisticsVO myTeamStatistics = teamCommissionService.queryMyTeamStatistics(this.getUser().getUserId(), type);
        return ok(null, myTeamStatistics);
    }

    @ApiOperation("交易记录（修改）")
    @GetMapping("/deal/record")
    public PageBean<DealRecordVO> dealRecord(Integer type) {

        DealRecordQuery query = new DealRecordQuery();
        query.setUserId(this.getUser().getUserId());
        query.setType(type);
        return dealRecordService.queryDealRecord(query);
    }

    @ApiOperation("交易记录")
    @GetMapping("/calculate/info")
    public Result<CalculateInfoVO> calculateInfo() {

        CalculateInfoVO calculateInfo = new CalculateInfoVO();

        //用户经纪人等级
        BrokerLevel brokerLevel = userInfoService.queryBrokerLevel(this.getUser().getUserId());
        calculateInfo.setBrokerLevel(brokerLevel);

        //经纪人系统设置
        BrokerCommissionSettingsDTO brokerCommissionSettings = systemSettingsService.queryBrokerCommissionSettingsInCache();
        BeanUtils.copyProperties(brokerCommissionSettings, calculateInfo);

        //团队系统设置
        TeamCommissionSettingsDTO teamCommissionSettings = systemSettingsService.queryTeamCommissionSettingsInCache();
        calculateInfo.setTeamCommissionFirstRate(teamCommissionSettings.getFirstRate());
        calculateInfo.setTeamCommissionSecondRate(teamCommissionSettings.getSecondRate());

        return ok(null, calculateInfo);
    }

}
